package com.wechat.login.util;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.wechat.login.pojo.TokenInfo;
import com.wechat.login.pojo.WeChatUser;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.net.http.HttpClient;

import static com.wechat.login.constans.LoginConstants.APP_ID;
import static com.wechat.login.constans.LoginConstants.APP_SECRET;
@Slf4j
@Component
public class WechatUtil {

    private static StringRedisTemplate redisTemplate;

    @Autowired
    public void setRedisTemplate(StringRedisTemplate redisTemplate) {
        WechatUtil.redisTemplate = redisTemplate;
    }



    public static WeChatUser getUserInfo(String code) throws IOException {
        String responseResult = "";
        System.out.println("code = " + code);
        RestTemplate restTemplate = new RestTemplate();
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String tokenUrl ="https://api.weixin.qq.com/sns/oauth2/access_token?appid="+APP_ID+"&secret="+APP_SECRET+"&code="+code+"&grant_type=authorization_code";
//        TokenInfo tokenInfo = restTemplate.getForObject(tokenUrl, TokenInfo.class);
//        log.info("获取到tokenInfo：{}",tokenInfo.toString());
        HttpGet request = new HttpGet(tokenUrl);
        CloseableHttpResponse response = httpClient.execute(request);
        if (response.getStatusLine().getStatusCode() == 200){
            responseResult = EntityUtils.toString(response.getEntity(),"UTF-8");
        }
        log.info("获取到responseResult：{}",responseResult);
        TokenInfo tokenInfo = JSONUtil.toBean(responseResult, TokenInfo.class);
        String userInfoURL = "https://api.weixin.qq.com/sns/userinfo?access_token="+tokenInfo.getAccessToken()+"&openid="
                +tokenInfo.getOpenid()+"&lang=zh_CN";
//        WeChatUser userInfo = restTemplate.getForObject(userInfoURL, WeChatUser.class);
        HttpGet request1 = new HttpGet(userInfoURL);
        CloseableHttpResponse response1 = httpClient.execute(request1);
        if (response1.getStatusLine().getStatusCode() == 200){
            responseResult = EntityUtils.toString(response1.getEntity(),"UTF-8");
        }
        log.info("获取到userInfoResult：{}",responseResult);
        WeChatUser userInfo = JSONUtil.toBean(responseResult, WeChatUser.class);
        redisTemplate.opsForValue().set("token:"+tokenInfo.getAccessToken(), JSONUtil.toJsonStr(userInfo));
        return userInfo;
    }
}
